package com.jvyou.app.ds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.utils.orm.WrapperTool;
import com.jvyou.app.ds.domain.dto.VehicleUsageDto;
import com.jvyou.app.ds.domain.entity.Lesson;
import com.jvyou.app.ds.domain.entity.Vehicle;
import com.jvyou.app.ds.domain.entity.VehicleUsage;
import com.jvyou.app.ds.mapper.LessonMapper;
import com.jvyou.app.ds.mapper.VehicleMapper;
import com.jvyou.app.ds.mapper.VehicleUsageMapper;
import com.jvyou.app.ds.service.VehicleUsageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 车辆使用记录表(VehicleUsage)表服务实现类
 *
 * @author 橘柚
 * @since 2025-04-16 17:48:05
 */
@Service("vehicleUsageService")
@RequiredArgsConstructor
public class VehicleUsageServiceImpl extends ServiceImpl<VehicleUsageMapper, VehicleUsage> implements VehicleUsageService {

    private final VehicleMapper vehicleMapper;
    private final LessonMapper lessonMapper;

    private QueryWrapper<VehicleUsage> getWrapper(VehicleUsageDto vehicleUsageDto) {
        QueryWrapper<VehicleUsage> queryWrapper = new QueryWrapper<>();
        WrapperTool.notEmptyEq(queryWrapper, "vehicle_id", vehicleUsageDto.getVehicleId());
        WrapperTool.notEmptyEq(queryWrapper, "coach_id", vehicleUsageDto.getCoachId());
        WrapperTool.notEmptyEq(queryWrapper, "lesson_id", vehicleUsageDto.getLessonId());
        WrapperTool.notEmptyEq(queryWrapper, "usage_purpose", vehicleUsageDto.getUsagePurpose());
        WrapperTool.notEmptyEq(queryWrapper, "usage_status", vehicleUsageDto.getUsageStatus());
        if (vehicleUsageDto.getStartTime() != null) {
            queryWrapper.ge("start_time", vehicleUsageDto.getStartTime());
        }
        if (vehicleUsageDto.getEndTime() != null) {
            queryWrapper.le("end_time", vehicleUsageDto.getEndTime());
        }
        return queryWrapper;
    }

    @Override
    public List<VehicleUsage> listVehicleUsage(VehicleUsageDto vehicleUsageDto) {
        return list(getWrapper(vehicleUsageDto));
    }

    @Override
    public PageResult<VehicleUsage> pageVehicleUsage(VehicleUsageDto vehicleUsageDto) {
        IPage<VehicleUsage> page = new Page<>(vehicleUsageDto.getCurrent(), vehicleUsageDto.getPageSize());
        page = page(page, getWrapper(vehicleUsageDto));
        List<VehicleUsage> vehicleUsages = page.getRecords();
        for (VehicleUsage vehicleUsage : vehicleUsages) {
            // 1.查询教练车信息
            Vehicle vehicle = vehicleMapper.selectById(vehicleUsage.getVehicleId());
            vehicleUsage.setVehicle(vehicle);
            vehicleUsage.setPlateNumber(vehicle.getPlateNumber());
            // 2.查询课程信息
            if (vehicleUsage.getLessonId() != null) {
                Lesson lesson = lessonMapper.selectById(vehicleUsage.getLessonId());
                vehicleUsage.setLesson(lesson);
            }
            // 3. TODO:查询教练信息
        }
        return PageResult.success(page);
    }
}
